<template>
  <div class="ele-body">
    <el-row :gutter="15">
      <el-col :md="6" :sm="8">
        <el-card shadow="never" body-style="padding: 25px;height:calc(100vh - 166px);">
          <div class="user-info-card">
            <div class="user-info-avatar-group" @click="showCropper=true">
              <img class="user-info-avatar" :src="form.avatar" alt=""/>
              <i class="el-icon-upload2"></i>
            </div>
            <h2 class="user-info-name">{{userInfo.realname}}</h2>
            <div class="user-info-desc">{{form.sign || 'N/A'}}</div>
          </div>
          <div style="margin: 30px 0 20px 0;">
            <el-divider class="ele-divider-dashed ele-divider-base"/>
          </div>
          <div class="user-info-list">
            <div class="user-info-item">
              <i class="el-icon-postcard"></i>
              <span>{{userInfo.position_name}}</span>
            </div>
            <div class="user-info-item">
              <i class="el-icon-office-building"></i>
              <span>{{userInfo.full_dept}}</span>
            </div>
            <div class="user-info-item">
              <i class="el-icon-location-information"></i>
              <span>中国 • 云南省 • 昆明市</span>
            </div>
            <div class="user-info-item">
              <i class="el-icon-_school"></i>
              <span>HTML、CSS、VUE、PHP、JAVA</span>
            </div>
          </div>

        </el-card>
      </el-col>
      <el-col :md="18" :sm="16">
        <el-card shadow="never" body-style="padding-top: 5px;height:calc(100vh - 136px);">
          <el-tabs v-model="active" class="user-info-tabs">
            <el-tab-pane label="基本信息" name="info">
              <el-form
                ref="infoForm"
                :rules="rules"
                :model="form"
                label-width="90px"
                style="max-width: 450px;padding-top: 40px;"
                @keyup.enter.native="save"
                @submit.native.prevent>
                <el-form-item label="用户名:" prop="username">
                  <el-input
                    v-model="form.username"
                    placeholder="请输入用户名"
                    clearable/>
                </el-form-item>
                <el-form-item label="性别:" prop="sex">
                  <el-select
                    v-model="form.sex"
                    placeholder="请选择性别"
                    class="ele-fluid"
                    clearable>
                    <el-option label="保密" :value="-1"/>
                    <el-option label="男" :value="1"/>
                    <el-option label="女" :value="0"/>
                  </el-select>
                </el-form-item>
                <el-form-item label="邮箱:" prop="email">
                  <el-input
                    v-model="form.email"
                    placeholder="请输入邮箱"
                    clearable/>
                </el-form-item>
                <el-form-item label="手机:">
                  <el-input
                    v-model="form.mobile"
                    placeholder="请输入手机"
                    clearable/>
                </el-form-item>
                <el-form-item label="签名:">
                  <el-input
                    v-model="form.sign"
                    placeholder="请输入签名"
                    :rows="4"
                    type="textarea"/>
                </el-form-item>
                <el-form-item>
                  <el-button
                    type="primary"
                    @click="save"
                    :loading="loading">保存更改
                  </el-button>
                </el-form-item>
              </el-form>
            </el-tab-pane>
          </el-tabs>
        </el-card>
      </el-col>
    </el-row>
    <!-- 头像裁剪弹窗 -->
    <ele-cropper-dialog
      :show.sync="showCropper"
      @crop="onCrop"
      :src="form.avatar"
      :lock-scroll="false"/>
  </div>
</template>

<script>
import util from '@/utils/util';
import EleCropperDialog from 'ele-admin/packages/ele-cropper-dialog';
import { mapState } from 'vuex';
import setting from '@/config/setting';
import OSS from 'ali-oss';
export default {
  name: 'UserInfo',
  components: {EleCropperDialog},
  computed: {
    ...mapState({
      userInfo: ({user}) => user.user,
    })
  },
  data() {
    return {
      // tab页选中
      active: 'info',
      ossClient: null,
      form:{},
      // 表单验证规则
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'}
        ],
        sex: [
          {required: true, message: '请选择性别', trigger: 'blur'}
        ],
        email: [
          {required: true, message: '请输入邮箱', trigger: 'blur'}
        ]
      },
      // 保存按钮loading
      loading: false,
      // 是否显示裁剪弹窗
      showCropper: false
    };
  },
  mounted() {
    this.reload();
    this.ossClient = new OSS(setting.oss);
  },
  methods: {
    reload(){
      this.form = {
        user_id:this.userInfo.user_id,
        username:this.userInfo.username,
        sex:this.userInfo.sex,
        email:this.userInfo.email,
        mobile:this.userInfo.mobile,
        sign:this.userInfo.sign,
        avatar:this.userInfo.avatar
      };
    },
    /* 保存更改 */
    save() {
      this.$refs['infoForm'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http.post('/app.user.info.edt', {info: JSON.stringify(this.form)}).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message({type: 'success', message: res.data.msg});
              this.$http.get(setting.userUrl).then((res) => {
                const result = setting.parseUser ? setting.parseUser(res.data) : res.data;
                if (result.code === 0) {
                  const user = result.data;
                  this.$store.dispatch('user/setUser', user);
                  // this.$store.dispatch('user/setRoles', user ? user.roles : null);
                  // this.$store.dispatch('user/setAuthorities', user ? user.authorities : null);
                } else if (result.msg) {
                  this.$message.error(result.msg);
                }
                this.reload();
              }).catch((e) => {
                this.$message.error(e.message);
              });

            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        }
      });
    },
    /* 头像裁剪完成回调 */
    onCrop(res) {
      this.form.avatar = res;
      this.showCropper = false;
      this.ossClient.put("avatar/"+this.userInfo.user_id, util.dataURLtoBlob(res)).then(rest=>{
        if (rest.res.statusCode === 200) {
          this.form.avatar = rest.url;
        }else {
          this.form.avatar = this.userInfo.avatar;
          this.$message.error('头像上传失败');
        }
      }).catch(err => {
        this.form.avatar = this.userInfo.avatar;
        this.$message.error('头像上传失败');
      });
    }
  }
}
</script>

<style scoped>
.ele-body {
  padding-bottom: 0;
}

.el-card {
  margin-bottom: 15px;
}

/* 用户资料卡片 */
.user-info-card {
  padding-top: 8px;
  text-align: center;
}

.user-info-card .user-info-avatar-group {
  position: relative;
  cursor: pointer;
  margin: 0 auto;
  width: 110px;
  height: 110px;
  border-radius: 50%;
  overflow: hidden;
}

.user-info-card .user-info-avatar {
  width: 110px;
  height: 110px;
  border-radius: 50%;
  object-fit: cover;
}

.user-info-card .user-info-avatar-group > i {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: #FFF;
  font-size: 30px;
  display: none;
  z-index: 2;
}

.user-info-card .user-info-avatar-group:hover > i {
  display: block;
}

.user-info-card .user-info-avatar-group:hover:after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, .3);
}

.user-info-card .user-info-name {
  font-size: 24px;
  margin-top: 20px;
}

.user-info-card .user-info-desc {
  margin-top: 8px;
}

/* 用户信息列表 */
.user-info-list {
  margin-top: 30px;
}

.user-info-list .user-info-item {
  margin-bottom: 10px;
  display: flex;
  align-items: baseline;
}

.user-info-item > i {
  margin-right: 10px;
  font-size: 16px;
}

.user-info-item > span {
  flex: 1;
  display: block;
}

/* 用户标签 */
.user-info-tags .el-tag {
  margin: 10px 10px 0 0;
}

/* 用户账号绑定列表 */
.user-account-list {
  margin-top: 10px;
}

.user-account-list .user-account-item {
  padding: 15px;
}

.user-account-list .user-account-item .ele-text-secondary {
  margin-top: 6px;
}

.user-account-list .user-account-item .user-account-icon {
  width: 42px;
  height: 42px;
  line-height: 42px;
  text-align: center;
  color: #FFF;
  font-size: 26px;
  border-radius: 50%;
  background-color: #3492ED;
  box-sizing: border-box;
}

.user-account-list .user-account-item .user-account-icon.el-icon-_wechat {
  background-color: #4DAF29;
  font-size: 28px;
}

.user-account-list .user-account-item .user-account-icon.el-icon-_alipay {
  background-color: #1476FE;
  padding-left: 5px;
  font-size: 32px;
}

/* tab页签 */
.user-info-tabs ::v-deep .el-tabs__nav-wrap {
  padding-left: 20px;
}
</style>
